package com.wr.add05;

import java.util.concurrent.CountDownLatch;

// 计数器  减法计数器
public class CountDownLatchDemo {
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(6);  // 设置一个6的计数器

        for (int i = 0; i < 6; i++) {
            new Thread(()->{
                System.out.println(Thread.currentThread().getName()+"--->GO");
                String name = Thread.currentThread().getName();
                if ("1".equals(name)){
                    try {
                        Thread.sleep(10000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                countDownLatch.countDown();  // 计数器-1，后面代码无需等待
                System.out.println(Thread.currentThread().getName()+"--->执行完毕");
            },String.valueOf(i)).start();
        }

        countDownLatch.await();  // 等待计数器归0执行下面的操作
        System.out.println("close Door");
    }
}
